import  {getOrder} from '../../../api/api.js';
import {sendMsg} from '../../utils/chat.js';
import {deepClone,formatTime} from '../../utils/util.js';
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    show:false,
    orders:[]
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onClose(){
      this.setData({
          show:false
      })
   },
   sendOrder(e){
       console.log(e.currentTarget.dataset.order,"发送订单==============")
        
       //关闭弹窗
        this.setData({
           show:false
        }) 
        
        let order = e.currentTarget.dataset.order;
        this.addOrder(order);

        // 发送消息
        this.toSendOrder(order);


   },


   

        // 把视频添加到页面上
        addOrder(order){
          
          let date = new Date();
          date =formatTime(date);
          //这样就和后端传递过来的事件一样长了。

          let item = {
            chatTime:date,
            initiator:"u",
          }

          let content = {
            type:"order",
            msg:{
               orderId:order.id,
               img:order.details[0].picPath,
               price:order.details[0].price,
               title:order.details[0].goodsName
            },
            
          }

          item.content = JSON.stringify(content);
          //获取当前页的对象
          let page = getCurrentPages()[getCurrentPages().length-1];
          //把page 当this用就完了。
          let histories =  page.data.histories;
          histories.push(item);
          page.setData({
            histories,
          })
          // //设置滚动到底部
          page.setToIndex()


          // ------------------------------------------------------------------

        },

       




      
      toSendOrder(order){
          let that = getCurrentPages()[getCurrentPages().length-1];
          if(that.data.selService.id){
              
          }else{
              wx.showToast({
                title:'没有客服哦!',
                icon:"none"
              })
              return;
          }
          // 获取选中的客服
          let item = that.data.selService;
          // 获取本地存储的用户信息
          let userInfo = wx.getStorageSync('Yuserinfo').userInfo;
        let data = {
          recipientId:item.id,//客服id
          uName:userInfo.nickName,//我的用户名
          headImage:userInfo.avatarUrl,//我的头像
          merchantId:that.data.merchantId//商家id
        }
        

        let message = {
          type:"order",
          msg:{
             orderId:order.id,
             img:order.details[0].picPath,
             price:order.details[0].price,
             title:order.details[0].goodsName
          },
          
        }
  
          // //发送图片 
          // let  message = {
          //     type:"video",
          //     msg:videoPath
          //   }
  
        message = JSON.stringify(message);
        data.message = message;
  
        //根据数组索引判断是不是主账号。
        if(item.index == 0){
          data.type = "merchant"
        }else{
          data.type = "subAccount"
        }
  
        console.log(data);
        
          sendMsg(data).then(res=>{
              console.log(res,"发送消息的回调函数");
              // 发送成功之后去渲染历史记录
          })
  },
  

















         
  },
 
  

  lifetimes: {
    created(){
     
 

    },
    attached() {
      console.log("attached在组件实例进入页面节点树时执行",this.data.flag);
      console.log("%c created在组件实例刚刚被创建时执行","font-size:50px;color:red") 
      let page = getCurrentPages()[getCurrentPages().length-1];
      let data = {
          openId:wx.getStorageSync('loginInfo').openid,
          merchantId:page.data.merchantId 
      }
      getOrder(data).then(res=>{
          console.log("%c获取用户的订单","color:red;font-size:50px",res)
          this.setData({
             orders:res.data.data.rows
          })


          console.log(this.data.orders)
      })

    },
    ready() {
      console.log("ready在组件在视图层布局完成后执行")
    },
    moved() {
      console.log("moved在组件实例被移动到节点树另一个位置时执行")
    },
    detached() {
      console.log("detached在组件实例被从页面节点树移除时执行")
    },
    error() {
      console.log("error每当组件方法抛出错误时执行")
    }
  }


  
})








